package com.qingyun.form.validator

import com.qingyun.form.items.BaseFormItem

/**
 * 必填项校验
 */
class RequiredFormValidator(private val validator: BaseFormValidator? = null) :
    BaseFormValidator() {
    override fun validate(
        item: BaseFormItem,
        value: String?,
    ): FormValidatorResult {
        val text = value?.trim()
        //如果输入的内容为空，则校验失败
        if (text.isNullOrEmpty()) {
            return error(item, "${item.label}不能为空")
        }
        //如果输入的内容不为空，则调用其他的校验器进行校验
        val r = validator?.validate(item, value)
        if (r?.successfully() == false) {
            return r
        }
        return success(item)
    }
}